from playwright.sync_api import Playwright, sync_playwright


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False, channel="chrome")
    context = browser.new_context(viewport={"width":1280,"height":720}, geolocation={"latitude":37.819722,"longitude":-122.478611}, permissions=["geolocation"], locale="zh-CN", color_scheme="light", timezone_id="Asia/Shanghai")

    # Open new page
    page = context.new_page()

    # Go to https://www.jd.com/
    page.goto("https://www.jd.com/")

    # Click text=你好，请登录
    page.click("text=你好，请登录")
    # assert page.url == "https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F"

    # Click [placeholder="邮箱/用户名/登录手机"]
    page.click("[placeholder=\"邮箱/用户名/登录手机\"]")

    # Fill [placeholder="邮箱/用户名/登录手机"]
    page.fill("[placeholder=\"邮箱/用户名/登录手机\"]", "18120691526")

    # Click [placeholder="密码"]
    page.click("[placeholder=\"密码\"]")

    # Fill [placeholder="密码"]
    page.fill("[placeholder=\"密码\"]", "love1263848268")

    # Click text=登 录
    page.click("text=登 录")

    # Click text=向右滑动完成拼图
    page.click("text=向右滑动完成拼图")

    # Click #JDJRV-wrap-loginsubmit div:has-text("完成拼图验证换一张向右滑动完成拼图")
    page.click("#JDJRV-wrap-loginsubmit div:has-text(\"完成拼图验证换一张向右滑动完成拼图\")")

    # Close page
    page.close()

    # Open new page
    page = context.new_page()

    # Go to https://www.jd.com/
    page.goto("https://www.jd.com/")

    # Go to https://passport.jd.com/
    page.goto("https://passport.jd.com/")

    # Close page
    page.close()

    # ---------------------
    context.storage_state(path="saveStorage.json")
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)
